<template>
  <div>
    <!--以下是审批字段-->
    <div v-show="type === 'todo'">
      <el-form label-width="100px">
        <el-divider></el-divider>
        <el-form-item label="审批意见">
          <el-radio-group v-model="pass">
            <el-radio label="true">同意</el-radio>
            <el-radio label="false">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="批注" prop="remark">
          <el-input v-model="comment" type="textarea" :value="comment" />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <el-button v-show="type === 'todo'" type="primary" @click="complete"
        >确 定</el-button
      >
      <el-button @click="close">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import { editPageValue } from "@/views/UDform/js/pageValue";

export default {
  name: "ViewVerify",
  props: {
    row: {
      type: Object
    },
    taskId: {
      type: String
    },
    type: {
      type: String
    },
    drawingList: {
      type: Array
    },
    id: {
      type: String
    }
  },
  data() {
    return {
      pass: "true",
      comment: "同意",
      ifJudge: false,
      addForm: {},
    };
  },
  methods: {
    compare() {
      for (let i = 0; i < this.drawingList.length; i++) {
        for (const key in this.row) {
          if ( this.drawingList[i].vModel == key && this.drawingList[i].defaultValue != this.row[key]) {
            this.ifJudge = true;
          }
        }
      }
    },
    close: function() {
      this.$emit("update:open", false);
    },
    complete() {
      // this.$parent.$children[0].validate(valid => {
      // if (valid) {
      this.compare();
      if (this.ifJudge == true) {
        this.submitForm();
      }
      const data = {
        taskId: this.taskId,
        instanceId: this.row.instanceId,
        variables: JSON.stringify({
          comment: this.comment,
          pass: this.pass,
          formData: this.row
        })
      };
      return request({
        url: "/activiti/process/complete",
        method: "post",
        params: data
      }).then(response => {
        this.close();
        // 刷新待办事项列表（丑陋的代码 👇）
        this.$parent.$parent.$parent.$parent.$parent.$parent.getList();
        this.msgSuccess("操作成功");
      });
      //        }else{
      // 	this.msgError("操作失败")
      // }
      // });
    },
    submitForm() {
      for (let i = 0; i < this.drawingList.length; i++) {
        let key = this.drawingList[i].vModel;
        let value = this.drawingList[i].defaultValue;
        this.addForm[key] = value;
      }
      let data = {};
      data.map = this.addForm;
      data.table = this.id;
      data.id = this.row.id;
      editPageValue(data);
    }
  },
  watch: {
    pass: function(val) {
      this.comment = val === "true" ? "同意" : "驳回";
    }
  },
};
</script>

<style scoped></style>
